#include "Stdafx.h"
#include "DrawingManager.h"

void DrawingManager::setDrawing(bool _isDrawing)
{
	isDrawing = _isDrawing;

}
//---------------------------------------------
bool DrawingManager::isDrawingBrush()
{
	return isDrawing;
}
//---------------------------------------------
void DrawingManager::endBrush()
{
	if(isDrawing)
	{
		isDrawing = false;
		brushNum++;
		cursorPoints.clear();
		drawingFootprintId = 0;
	}
}

//---------------------------------------------
void DrawingManager::addFootprint( Ogre::Real x , Ogre::Real y, Ogre::Real dryness )
{
	// Load head image
	char cName[30];
	char cPath[50];


	sprintf(cName, "footprint%i", footprintid);
	footprintid++;

	Ogre::String name(cName);
	mCamView = mCamera->getViewport();
	DynamicImage* footprintImg;

	footprintImg = new DynamicImage(mSceneMgr,
		name,
		mCamera,
		Ogre::Vector3(x,y,0.5f),
		Ogre::Vector3(255.0f,0,0),
		Ogre::Vector2(1,1),
		true,
		true,
		0);

	//footprintImg->openImage(cPath);

	switch(useStroke){
	case 0:
		footprintImg->useSpecifiedTextures("footprintMaterial01", "footprintTexture01");
		break;
	case 1:
		footprintImg->useSpecifiedTextures("footprintMaterial02", "footprintTexture02");
		break;
	case 2:
		footprintImg->useSpecifiedTextures("footprintMaterial03", "footprintTexture03");
		break;
	default:
		break;
	}

	footprintsVector.push_back(footprintImg);

	//footprintImg->createImage(footprintImg->img_width, footprintImg->img_height, footprintImg->img_data, Ogre::PixelFormat::PF_A8R8G8B8);

}

//---------------------------------------------
void DrawingManager::addTrailRendering( Ogre::Real x , Ogre::Real y )
{
	int trailLength = 60;
	if(trailRenderingId >= trailLength){
		trailRenderingId = 0;
		clearBuffer = true;
	}

	if(clearBuffer){
		DynamicImage* front = trailRenderingVector.front();
		trailRenderingVector.pop_front();
		delete front;
	}

	// Load head image
	char cName[30];

	sprintf(cName, "trailRendering_%i", trailRenderingId);
	trailRenderingId++;

	Ogre::String name(cName);
	mCamView = mCamera->getViewport();
	DynamicImage* trailRenderingImg;

	trailRenderingImg = new DynamicImage(mSceneMgr,
		name,
		mCamera,
		Ogre::Vector3(x,y,0.5f),
		Ogre::Vector3(255.0f,0,0),
		Ogre::Vector2(1,1),
		true,
		true,
		0);


	trailRenderingImg->useSpecifiedTextures("TrailRendererTex", "TrailRendererTex");
	trailRenderingVector.push_back(trailRenderingImg);
}

//---------------------------------------------
void DrawingManager::drawChineseBrush()
{
	//char manualName[15];
	//sprintf(manualName, "brushNum%d", brushNum);
	//char nodeName[15];
	//sprintf(nodeName, "nodeNum%d", brushNum);
	//printf("%s\n", manualName);

	//if (mSceneMgr->hasSceneNode(nodeName))
	//	mSceneMgr->destroySceneNode(nodeName);
	//if (mSceneMgr->hasManualObject(manualName))
	//	mSceneMgr->destroyManualObject(manualName);
	//Ogre::ManualObject* manual = mSceneMgr->createManualObject(Ogre::String(manualName));

	//manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
	//manual->setUseIdentityProjection(true);
	//manual->setUseIdentityView(true);

	//for(int i=0;i<cursorPoints.size();i++)
	//{
		// manual->position(cursorPoints[i].x, cursorPoints[i].y, 0.0);
		if(drawingFootprintId != 0){
			for(int i = drawingFootprintId ; i<cursorPoints.size() ; i++){
				Ogre::Vector2 prePoint(cursorPoints[i-1].x, cursorPoints[i-1].y);
				Ogre::Vector2 curPoint(cursorPoints[i  ].x, cursorPoints[i  ].y);
				float disBetweenPoint = prePoint.distance(curPoint);

				// std::cout << "Distance between point : " << disBetweenPoint << std::endl;

				float disThreshold;
				if(useStroke == 0)
					disThreshold = 0.02f;
				else if(useStroke == 1)
					disThreshold = 0.02f;
				else if(useStroke == 2)
					disThreshold = 0.01f;

				if(disBetweenPoint > disThreshold){
					int interpolationCount = disBetweenPoint/disThreshold;
					// std::cout << "=== > Interpolation point count  : " << interpolationCount << std::endl;
					for(int j=1 ; j<= interpolationCount; j++){
						Ogre::Vector2 interpolationPoint;
						interpolationPoint = ((float)j/interpolationCount)*(prePoint) +  (1-((float)j/interpolationCount))*(curPoint);
						this->addFootprint(interpolationPoint.x, interpolationPoint.y,0.0);
					}
				}
				else{
					this->addFootprint(cursorPoints[i].x, cursorPoints[i].y, 0.0);
				}
			}
		}
		else{
			this->addFootprint(cursorPoints[0].x, cursorPoints[0].y ,0.0);
		}

		drawingFootprintId++;
	//}

	//manual->end();
	//mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
}
//---------------------------------------------
void DrawingManager::update(float timeSinceLastFrame, Ogre::Vector2 mousePosition, int _useStroke)
{
	useStroke = _useStroke;

	float mouseX = mousePosition.x *2-1;
	float mouseY = -(mousePosition.y*2-1);

	if(isDrawing) cursorPoints.push_back(Ogre::Vector3(mouseX, mouseY, 0));
	if(isDrawing) drawChineseBrush();
}
//---------------------------------------------
bool DrawingManager::clearAllBrush()
{
	for(int i = 0 ; i<footprintsVector.size() ; i++){
		/*DynamicImage* footprintImg;
		footprintImg = footprintsVector[i];

		footprintImg->~DynamicImage();*/
		delete footprintsVector[i];
	}
	footprintsVector.clear();

	footprintid = 0;
	return true;
}

void DrawingManager::initMaterial( Ogre::String materialName, Ogre::String textureName, Ogre::String path )
{
	Ogre::TexturePtr mOgreTexture = Ogre::TextureManager::getSingleton().create(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); 
	mOgreTexture->setNumMipmaps(0);
	
	Ogre::Image img;
	img.load(path, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	mOgreTexture->loadImage(img);

	Ogre::MaterialPtr mMaterial = Ogre::MaterialManager::getSingleton().create(materialName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	mMaterial->getTechnique(0)->getPass(0)->setCullingMode(Ogre::CullingMode::CULL_NONE);
	mMaterial->setReceiveShadows(false);
	mMaterial->setLightingEnabled(false);
	mMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(textureName);
	mMaterial->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
	mMaterial->getTechnique(0)->getPass(0)->setDepthCheckEnabled(false);
}
//---------------------------------------------